﻿/*
 * 2023/3/10
 * 订单及订单明细
 * 
 * **/
using Max.PH5_2008A.ShoppingCode.Domain.Shared;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2008A.ShoppingCode.Domain
{
    /// <summary>
    /// 订单主表
    /// </summary>
    [Table("tb_order_master")]
    public class OrderMasterEntity
    {
        public Guid Id { get; set; }

        private string _orderNo;
        public string OrderNo  //订单编号
        {
            get {
                string no = CreateOrderNo();
                if(string.IsNullOrEmpty(_orderNo))
                {
                    _orderNo = no;
                }
                return _orderNo; 
            }
            set { 
                _orderNo = value; 
            }
        }
        
        public Guid MemberId { get; set; } //会员ID

        public DateTime? OrderTime { get; set; } //下单时间
        public decimal? OrderTotal { get; set; }//订单金额
        public OrderStatue? OrderStatue { get; set; } = Shared.OrderStatue.待付款; // 订单状态

        public PayType? PayType { get; set; }//支付方式
        public decimal? PayTotal { get; set; }//支付金额
        public DateTime? PayTime { get; set; } //支付时间

        public decimal? RateTotal { get; set; } //优惠、折扣金额

        public string? RecivedName { get; set; } //收件人姓名
        public string? RecivedPhone { get; set; } //收件人电话
        public string? RecivedAddress { get; set; } //收件人地址

        public string? Remark { get; set; }

        //审计字段
        public DateTime? CreatedTime { get; set; } = System.DateTime.UtcNow;
        public string? CreatedBy { get; set; }
        public string? UpdatedBy { get; set; }
        public DateTime? UpdatedTime { get; set; }   


        /// <summary>
        /// 生成订单号
        /// </summary>
        /// <returns></returns>
        private string CreateOrderNo()
        {
            //P、M + 日期时间（20230310143345） + 流水号（随机数 4位）
            string orderFrom = "P";
            string date = System.DateTime.Now.ToString("yyyyMMddHHmmss");
            string no = new Random().Next(1000, 9999).ToString();

            return $"{orderFrom}{date}{no}";
        }


    }

    /// <summary>
    /// 订单明细表（记录这笔订单中的商品信息。和主表的关系是：1主多明细）
    /// </summary>
    [Table("tb_order_detail")]
    public class OrderDetailEntity
    {
        public Guid Id { get; set; }
        public string OrderNo { get; set; } //冗余字段的设计
        public Guid? SKUId { get; set; }
        public decimal Price { get; set; }
        public int Number { get; set; }
        public string Statue { get; set; } //这个状态是指：对你购买到的这个商品进行操作，如退货，退款，换货
    }
}
